/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package btnhom;

/**
 *
 * @author lhqbao
 */
import java.io.File;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import java.io.*;
import java.util.ArrayList;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

public class XML {
    
    private String ServerName;
    private String ServiceName;
    private String UserName;
    private String Password;
    private String Port;
    private ArrayList<String> rolev;
    
    
    private String getTextValue(String def, Element doc, String tag) 
    {
        String value = def;
        NodeList nl;
        nl = doc.getElementsByTagName(tag);
        if (nl.getLength() > 0 && nl.item(0).hasChildNodes()) {
            value = nl.item(0).getFirstChild().getNodeValue();
        }
        return value;
    }
    
    public ArrayList XMLRead() throws Exception{
        
        //The two lines below are just for getting an
        //instance of DocumentBuilder which we use
        //for parsing XML data
        rolev = new ArrayList<String>();
        DocumentBuilderFactory factory =
            DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();

        //Here we do the actual parsing
        Document doc = builder.parse(new File("src/conn/cauhinh.xml"));

        //Here we get the root element of XML and print out
        //the value of its 'testAttr' attribute
        Element rootElement = doc.getDocumentElement();
        int count = rootElement.getElementsByTagName("OConnection").getLength();
        
        ServerName = getTextValue(ServerName, rootElement, "ServerName");
        ServiceName = getTextValue(ServiceName, rootElement, "ServiceName");
        UserName = getTextValue(UserName, rootElement, "UserName");
        Password = getTextValue(Password, rootElement, "Password");
        Port = getTextValue(Port, rootElement, "Port");
        
        if (ServerName != null) {
                if (!ServerName.isEmpty())
                    rolev.add(ServerName);
        }
        if (ServiceName != null) {
                if (!ServiceName.isEmpty())
                    rolev.add(ServiceName);
        }
        if (UserName != null) {
                if (!UserName.isEmpty())
                    rolev.add(UserName);
        }
        if (Password != null) {
                if (!Password.isEmpty())
                    rolev.add(Password);
        }
        if (Port != null) {
                if (!Port.isEmpty())
                    rolev.add(Port);
        }
       return rolev;  
    }
    public static void XMLWrite() throws Exception{
        
        DocumentBuilderFactory factory =
            DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();

        // Here instead of parsing an existing document we want to
        // create a new one.
        Document testDoc = builder.newDocument();

        // This creates a new tag named 'testElem' inside
        // the document and sets its data to 'TestContent'
        Element el = testDoc.createElement("testElem");
        el.setTextContent("TestContent");
        testDoc.appendChild(el);

        // The XML document we created above is still in memory
        // so we have to output it to a real file.
        // In order to do it we first have to create
        // an instance of DOMSource
        DOMSource source = new DOMSource(testDoc);

        // PrintStream will be responsible for writing
        // the text data to the file
        PrintStream ps = new PrintStream("/home/lhqbao/Documents/CSDLHDT/trunk/src/conn/test2.xml");
        StreamResult result = new StreamResult(ps);

        // Once again we are using a factory of some sort,
        // this time for getting a Transformer instance,
        // which we use to output the XML
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();

        // The actual output to a file goes here
        transformer.transform(source, result);
    }   
    
}
